//
//  XHREssenceViewController.m
//  BuDeJie
//
//  Created by haoran on 16/3/31.
//  Copyright © 2016年 xuhaoran. All rights reserved.
//

#import "XHREssenceViewController.h"
#import "XHRTittleButton.h"
#import "XHRAllViewController.h"
#import "XHRPictureViewController.h"
#import "XHRVideoViewController.h"
#import "XHRVoiceViewController.h"
#import "XHRWordViewController.h"


@interface XHREssenceViewController ()<UIScrollViewDelegate>

/** 标题按钮滚动栏 **/
@property (nonatomic, weak) UIView * tittleView;
/** 上一次选中的按钮 **/
@property (nonatomic, weak) XHRTittleButton * previousClickedTittleButton;
/** 标题下划线 **/
@property (nonatomic, weak) UIView * tittleUnderLine;
/** 用来显示所有子控制器View的scrollView **/
@property (nonatomic, weak) UIScrollView * scrollView;

@end

@implementation XHREssenceViewController

#pragma mark - 初始化
- (void)viewDidLoad {
    [super viewDidLoad];
    // 设置背景色
    self.view.backgroundColor = [UIColor whiteColor];
    
    // 设置导航条内容
    [self setupNavBar];
    
    // 初始化子控制器
    [self setupChildVC];
    
    // 添加scrollView
    [self  setupScrollView];
    
    // 添加标题栏
    [self setupTittleView];
    
    // 添加第一个子控制器的View
    [self addChildVcViewtoScrollView];
    
   
    
}
/**
 *  初始化子控制器
 */
-(void)setupChildVC{
    
    [self addChildViewController:[[XHRAllViewController alloc]init]];
    [self addChildViewController:[[XHRVideoViewController alloc]init]];
    [self addChildViewController:[[XHRVoiceViewController alloc]init]];
    [self addChildViewController:[[XHRPictureViewController alloc]init]];
    [self addChildViewController:[[XHRWordViewController alloc]init]];
}

/**
 *  添加scrollView
 */
-(void)setupScrollView{
    
    //// 不要去自动调整scrollView的内边距
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    UIScrollView * scrollView = [[UIScrollView alloc]initWithFrame:self.view.bounds];
    scrollView.backgroundColor = XHRColor(134, 187, 152);
    [self.view addSubview:scrollView];
    
    NSInteger count = self.childViewControllers.count;
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.showsHorizontalScrollIndicator = NO;
    // 设置contentSize 才能滚动
    scrollView.contentSize = CGSizeMake(count * XHRScreenW, 0);
    // 设置分页功能
    scrollView.pagingEnabled = YES;
    // 设置代理
    scrollView.delegate = self;
    // 点击状态栏时,不要让scrollView滚回顶部
    scrollView.scrollsToTop = NO;
    
    self.scrollView = scrollView;

}

/**
 *  添加标题栏
 */
-(void)setupTittleView{
    
    UIView * tittleView = [[UIView alloc]init];
    tittleView.frame = CGRectMake(0, 64, XHRScreenW, 35);
    tittleView.backgroundColor = [UIColor colorWithWhite:1 alpha:0.5];
    [self.view addSubview:tittleView];
    self.tittleView = tittleView;
    
    // 设置标题按钮
    [self setupTittleButtons];
    
    // 设置下划线
    [self setupTitleUnderline];
}

/**
 *  设置下划线
 */
-(void)setupTitleUnderline{
    // 取出第一个按钮
    XHRTittleButton * firstTittleButton = self.tittleView.subviews.firstObject;
    
    UIView * tittleUnderLine = [[UIView alloc]init];
    CGFloat tittleUnderLineH = 2;
    CGFloat tittleUnderLineY = self.tittleView.xhr_height - tittleUnderLineH;
    tittleUnderLine.frame = CGRectMake(0, tittleUnderLineY, 0, tittleUnderLineH);
    tittleUnderLine.backgroundColor = [firstTittleButton titleColorForState:UIControlStateSelected];
    [self.tittleView addSubview:tittleUnderLine];
    self.tittleUnderLine = tittleUnderLine;
   
    // 新点击的按钮变成红色
    firstTittleButton.selected = YES;
    self.previousClickedTittleButton = firstTittleButton;
    
    // 下划线
    [firstTittleButton.titleLabel sizeToFit];
    self.tittleUnderLine.xhr_width = firstTittleButton.titleLabel.xhr_width + 10 ;
    self.tittleUnderLine.xhr_CenterX = firstTittleButton.xhr_CenterX;
}


/**
 *  设置标题按钮
 */
-(void)setupTittleButtons{

    NSArray *tittles = @[@"全部", @"视频", @"声音", @"图片", @"段子"];
    NSInteger count = tittles.count;
    
    // 计算每个button的宽和高
    CGFloat tittileButtonW = XHRScreenW / count;
    CGFloat tittleButtonH = self.tittleView.xhr_height;
    
    for (int i = 0; i < count; i++) {
        XHRTittleButton * btn = [[XHRTittleButton alloc]init];
        // 设置文字
        [btn setTitle:tittles[i] forState:UIControlStateNormal];
        // 添加点击事件
        [btn addTarget:self action:@selector(tittleButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        
        // 设置frame
        btn.frame = CGRectMake(i * tittileButtonW, 0, tittileButtonW, tittleButtonH);
        [self.tittleView addSubview:btn];
        
        // 设置tag
//        btn.tag = i;
        
    }
    

}


/**
 *  设置导航栏
 */
-(void)setupNavBar{
    
    // 左边
    UIBarButtonItem *leftItem = [UIBarButtonItem buttonItemWithImage:[UIImage imageNamed:@"nav_item_game_icon"] andWithHighlightImage:[UIImage imageNamed:@"nav_item_game_click_icon"] addTarget:self andAction:@selector(game)];
    
    self.navigationItem.leftBarButtonItem = leftItem;
    
    // 中间 titleView
    self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"MainTitle"]];
    
    // 右边
    UIBarButtonItem *rightItem = [UIBarButtonItem buttonItemWithImage:[UIImage imageNamed:@"navigationButtonRandom"] andWithHighlightImage:[UIImage imageNamed:@"navigationButtonRandomClick"] addTarget:nil andAction:nil];
    
    self.navigationItem.rightBarButtonItem = rightItem;
    
    
}

#pragma mark - 监听点击
/**
 *  标题按钮点击事件
 */
-(void)tittleButtonClick:(XHRTittleButton *)tittleButton {
    
    // 判断是否重复点击
    if (self.previousClickedTittleButton == tittleButton) {
        XHRLog(@"标题栏按钮发通知说被重复点击");
        [[NSNotificationCenter defaultCenter] postNotificationName:XHRTittleButtonDidRepeatClickNotification object:nil];
    }
    
    // 还原上一次按钮状态
    self.previousClickedTittleButton.selected = NO;
    // 设置本次按钮状态
    tittleButton.selected = YES;
    
    self.previousClickedTittleButton = tittleButton;
    
    // 获取该控件在标题View子控件的索引
    NSInteger index = [self.tittleView.subviews indexOfObject:tittleButton];
    
    [UIView animateWithDuration:0.25 animations:^{
        
        // 处理下划线
        self.tittleUnderLine.xhr_width = tittleButton.titleLabel.xhr_width + 10;
        self.tittleUnderLine.xhr_CenterX = tittleButton.xhr_CenterX;
        
        // 滑动scrollView到指定的子控制器界面
        self.scrollView.contentOffset = CGPointMake(index * self.scrollView.xhr_width, self.scrollView.contentOffset.y);
        
    }completion:^(BOOL finished) {
        
        // 添加index位置的子控制器view到scrollView中
        [self addChildVcViewtoScrollView];
        
        // 控制scrollView的scrollsToTop属性
        for (int i = 0; i < self.childViewControllers.count ; i++) {
            
            UIViewController *childVc = self.childViewControllers[i];
            
            // 如果控制器的view没有被创建,跳过
            if (!childVc.isViewLoaded) continue;
                
            // 如果控制器的View不是scrollView,跳过
            if (![childVc.view isKindOfClass:[UIScrollView class]]) continue;
            
            UIScrollView *scrollView = (UIScrollView *)childVc.view;
             // 被点击按钮对应的子控制器
            scrollView.scrollsToTop = (i == index);
        }
        
    }];
    
}


-(void)game{
    
    XHRFunction
}



#pragma mark - 其他
// 添加index位置的子控制器view到scrollView中
-(void)addChildVcViewtoScrollView{
    
    // 根据偏移量计算出索引
    NSInteger index = self.scrollView.contentOffset.x / self.scrollView.xhr_width;
    
    UIViewController * childVc = self.childViewControllers[index];
    
    if (childVc.isViewLoaded) return;
//    if (childVc.view.superview) return;
//    if (childVc.view.window) return;
//    if ([self.scrollView.subviews containsObject:childVc.view]) return;
    //view.frame = CGRectMake(self.scrollView.contentOffset.x, 0, self.scrollView.xhr_width, self.scrollView.xhr_height);
    
    //view.frame = CGRectMake(index * self.scrollView.xhr_width, 0, self.scrollView.xhr_width, self.scrollView.xhr_height);
   
    childVc.view.frame = self.scrollView.bounds;

    [self.scrollView addSubview:childVc.view];

}


#pragma mark - UIScrollViewDelegate代理方法
// 停止拖拽
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    
//    XHRFunction
}

// 停止减速(完全停止)
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{

    // 获取偏移量得到索引
    NSInteger index = scrollView.contentOffset.x / scrollView.xhr_width ;
    
    // 设置对应的按钮
    XHRTittleButton * btn = self.tittleView.subviews[index];
    
    // 如果上一次点击的按钮 和 这次想要点击的按钮 相同，直接返回
    if (btn == self.previousClickedTittleButton) return;
        
    [self tittleButtonClick:btn];
}

@end
